/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：TradeScreen.java <br>
 * 创建时间：2013-12-22 下午9:54:07 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui;

import com.orhanobut.logger.Logger;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.MainCharTile;
import lostland.gmud.exv2.MapScreen;
import lostland.gmud.exv2.MapTile;
import lostland.gmud.exv2.data.Item;
import lostland.gmud.exv2.data.Skill;
import lostland.gmud.exv2.ui.core.ScrollableMenuScreen;

/**
 * 类名：TradeScreen <br>
 * 说明：交易、拜师和请教
 *
 * @author 12548
 */
public class TradeScreen extends ScrollableMenuScreen {


	TalkingWindow topWindow;
	AutoWindow bottomWindow;

	int npcid;
	int trading;

	public static final int BTMWIDTH = 160;

	public TradeScreen(int npcid, int trading) {
		super(gets(npcid, trading), 20, 30, 120, 6, false);
		this.npcid = npcid;
		this.trading = trading;
		String ss;
		switch (trading) {
			case 1:
				ss = "要买什么你自己看看吧！";
				break;
			case 2:
				ss = "有什么不用的东西就拿来吧！";
				break;
			case 101:
				ss = "你想学什么就说吧！";
				break;
			case 102:
				ss = "请选择你要阅读的内容：";
				break;
			default:
				ss = "baga";
				break;
		}
		topWindow = new TalkingWindow(ss);
		bottomWindow = new AutoWindow(nborder.x + 100, nborder.y + nborder.height + 2, BTMWIDTH, 26, sticking[cursor] + "");
		refresh();
	}


	public TradeScreen(int npcid, int trading, String sss) {
		super(gets(npcid, trading), 20, 30, 120, 6, false);
		this.npcid = npcid;
		this.trading = trading;
		topWindow = new TalkingWindow(sss);
		bottomWindow = new AutoWindow(nborder.x + 100, nborder.y + nborder.height + 2, BTMWIDTH, 26, sticking[cursor] + "");
		refresh();
	}


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#drawbg()
	 */
	@Override
	public void drawbg() {

		if (buttons.length < 1) {
			onCancel();
		}

		MapScreen.getInstance().getMap().draw(MapTile.getInstance(), MapScreen.X, MapScreen.Y);
		MainCharTile.getInstance().drawInMap(MainCharTile.getInstance().currentDirection,
				MainCharTile.getInstance().currentStep, MainCharTile.getInstance().X, MainCharTile.getInstance().Y);

		topWindow.draw();
		bottomWindow.draw();
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onCancel()
	 */
	@Override
	public void onCancel() {
		getGame().popScreen();
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onClick(int)
	 */
	@Override
	public void onClick(int i) {
		switch (trading) {
			case 1:
				if (Gmud.mc.gold >= Gmud.getWp().get(sticking[i]).getCost()) {
					if (Item.notNull(Gmud.mc.give(sticking[i]))) {
						Gmud.mc.earnMoney(-Gmud.getWp().get(sticking[i]).getCost());
						refresh();
					} else {
						new SimpleNotificationScreen("你的物品栏已满！").pushToGame();
					}
				}
				break;
			case 2:
				if (!Gmud.mc.only(Gmud.mc.inventory.get(sticking[i]))) {
					Gmud.mc.earnMoney((int) (Gmud.mc.inventory.get(sticking[i]).getCost() * 0.7f));
					Gmud.mc.drop(Gmud.mc.inventory.get(sticking[i]));
					re();
				}
				break;
			case 101:
			case 102:
				if (!Gmud.mc.expcanlearn(Gmud.mc.skills[sticking[i]] + 1)) {
					new NotificationScreen("你的武学经验不足，无法领会更深的功夫").pushToGame();
				} else if (Gmud.mc.skills[sticking[i]] > Gmud.getNpc(npcid).skills[sticking[i]]) {
					new NotificationScreen("你的功夫已经不输为师了，真是可喜可贺呀！").pushToGame();
				} else if (Gmud.mc.potential == 0) {
					new NotificationScreen("你的潜能已经发挥到极限了").pushToGame();
					return;
				} else {
					new LearningScreen(sticking[i], Gmud.getNpc(npcid).skills[sticking[i]]).pushToGame();
					refresh();
					return;
				}
				break;
			default:
		}
	}

	static String t[];
	static int sticking[] = new int[1000];

	static String[] gets(int npcid, int trading) {

		int i;
		switch (trading) {
			case 1:
				t = new String[Gmud.getNpc(npcid).sells.length];
				for (i = 0; i < Gmud.getNpc(npcid).sells.length; i++) {
					sticking[i] = Gmud.getNpc(npcid).sells[i];
					t[i] = Gmud.getWp().get(sticking[i]).getName();
				}
				break;
			case 2:
				int k;
				k = 0;
				for (i = 0; i < Gmud.mc.inventory.size(); i++) {
					if (Item.notNull(Gmud.mc.inventory.get(i))) {
						k++;
					}
				}
				t = new String[k];
				k = 0;
				for (i = 0; i < Gmud.mc.inventory.size(); i++) {
					if (Item.notNull(Gmud.mc.inventory.get(i))) {
						sticking[k] = i;
						String suffix = " x " + Gmud.mc.get_pile(Gmud.mc.inventory.get(i));
						if (Gmud.mc.inventory.get(i).getTemplate().getPile() < 2) {
							suffix = "";
						}
						t[k] = Gmud.mc.inventory.get(i).getName() + suffix;
						k++;
					}
				}
				break;
			case 101:
			case 102:
				int tm;
				tm = 0;
				if (npcid == 6) {
					int skillsckd[] = {-1, -1, -1, -1, -1};
					Gmud.getNpc(npcid).skillsckd = skillsckd;
					int skills[] = {250, 250, 250, 250, 250, 250, 250, 250, 250};
					while (skills.length < Skill.getSkillCount()) {
						skills = Gmud.push_back(skills, 0);
					}
					Gmud.getNpc(npcid).skills = skills;
				}
				for (i = 0; i < Gmud.getNpc(npcid).skills.length; i++) {
					int j = Gmud.getNpc(npcid).skills[i];
					if (j > 0 && !Gmud.skill[i].npc_skill) {
						tm++;
					}
				}
				t = new String[tm];
				tm = 0;
				for (int j = 0; j < Gmud.getNpc(npcid).skills.length; j++)
					if (Gmud.getNpc(npcid).skills[j] > 0 && !Gmud.skill[j].npc_skill) {
						sticking[tm] = j;
						t[tm] = Gmud.skill[j].name + "×" + Gmud.getNpc(npcid).skills[j];
						tm++;
					}
				//FIXME 重写
				break;
			default:
				return new String[0];


		}
		Logger.w("GO");

		return t;
	}


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.DialogScreen#isStable()
	 */
	@Override
	public boolean isStable() {
		return false;
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#refresh()
	 */
	@Override
	public void refresh() {
		super.refresh();
		if (buttons.length < 1)
			return;
		switch (trading) {
			case 1:
				bottomWindow.text = "售价：" + Gmud.getWp().get(sticking[cursor]).getCost() + ("   当前数量：" + Gmud.mc.get_pile(Item.generateItem(sticking[cursor], -1))) + "\n金钱：" + Gmud.mc.gold;
				break;
			case 2:
				bottomWindow.text = "售价：" + (int) (Gmud.mc.inventory.get(sticking[cursor]).getCost() * 0.7f) + "\n金钱：" + Gmud.mc.gold;
				break;
			case 101:
			case 102:
				bottomWindow.text = "当前等级：" + Gmud.mc.skills[sticking[cursor]] + "\n潜能：" + Gmud.mc.potential;
				break;
			default:
		}
	}


	protected void re() {
		super.re(gets(npcid, trading), 20, 30, 120, 6);
		if (buttons.length < 1) {
			onCancel();
		}
		bottomWindow.y = nborder.y + nborder.height + 2;
		refresh();
	}


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.FullScreen#resume()
	 */
	@Override
	public void resume() {
		super.resume();
		refresh();
	}


}
